package com.huan.rabbitmq.callback;

import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;

import com.huan.rabbitmq.producer.RabbitProducer;

/**
 * 生产者消息确认
 *
 * @author huan.fu
 * @date 2018/11/5 - 13:55
 */
@Slf4j
public class RabbitConfirmCallback implements RabbitTemplate.ConfirmCallback {

	@Autowired
	private RabbitTemplate rabbitTemplate;
	String exchange = "exchange-rabbit-springboot-advance";
	String routingKey = "product";
	
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		log.info("(start)生产者消息确认=========================");
		log.info("correlationData:[{}]", correlationData);
		log.info("ack:[{}]", ack);
		log.info("cause:[{}]", cause);
		if (!ack) {
			log.info("消息可能未到达rabbitmq服务器");
			//重新发送或存数据库手动处理，这里 是重新发送
			String message = (String) RabbitProducer.sendMap.get(correlationData.getId());
			rabbitTemplate.convertAndSend(exchange, routingKey, message, new CorrelationData("routing" + UUID.randomUUID().toString()));
		}
		log.info("(end)生产者消息确认=========================");
		//对发送成功的数据删除键值绑定
		RabbitProducer.sendMap.remove(correlationData.getId());
		
	}
}
